import os
import subprocess
import platform
from typing import Dict, Any
try:
    from .utils import EnhancedOutput as Debug, OutputType as DebugType
except ImportError:
    from utils import EnhancedOutput as Debug, OutputType as DebugType

class SystemCommandTool:
    name = "system_command"
    description = "执行系统命令。可以在当前工作目录执行任意系统命令"
    parameters = {
        "type": "object",
        "properties": {
            "command": {
                "type": "string",
                "description": "要执行的命令"
            }
        },
        "required": ["command"]
    }

    def execute(self, args: Dict[str, Any]) -> Dict[str, Any]:
        try:
            command = args["command"]
            
            Debug.display(f"执行命令: {command}", DebugType.INFO)
            
            # 使用subprocess执行命令
            result = subprocess.run(
                command,
                shell=True,
                capture_output=True,
                text=True,
                encoding='utf-8'
            )
            
            stdout = result.stdout
            stderr = result.stderr
            
            return {
                "success": result.returncode == 0,
                "stdout": stdout if stdout else "",
                "stderr": stderr if stderr else ""
            }
        except Exception as e:
            return {
                "success": False,
                "stdout": "",
                "stderr": f"执行命令失败: {str(e)}"
            }

class CurrentDirectoryTool:
    name = "current_directory"
    description = "获取当前工作目录。显示当前工作目录的完整路径"
    parameters = {
        "type": "object",
        "properties": {},
        "required": []
    }

    def execute(self, args: Dict[str, Any]) -> Dict[str, Any]:
        try:
            current_dir = os.getcwd()
            return {
                "success": True,
                "stdout": current_dir,
                "stderr": ""
            }
        except Exception as e:
            return {
                "success": False,
                "stdout": "",
                "stderr": f"获取当前目录失败: {str(e)}"
            }

class SystemInfoTool:
    name = "system_info"
    description = "获取系统信息。显示操作系统、Python版本等系统信息"
    parameters = {
        "type": "object",
        "properties": {},
        "required": []
    }

    def execute(self, args: Dict[str, Any]) -> Dict[str, Any]:
        try:
            import sys
            import os
            
            info = []
            info.append(f"操作系统: {platform.system()} {platform.release()}")
            info.append(f"平台: {platform.platform()}")
            info.append(f"架构: {platform.machine()}")
            info.append(f"处理器: {platform.processor()}")
            info.append(f"Python版本: {sys.version}")
            info.append(f"当前工作目录: {os.getcwd()}")
            
            return {
                "success": True,
                "stdout": "\n".join(info),
                "stderr": ""
            }
        except Exception as e:
            return {
                "success": False,
                "stdout": "",
                "stderr": f"获取系统信息失败: {str(e)}"
            }